<template>
  <div class="user-profile-container">
    <!-- 用户信息头部 -->
    <div class="user-header">
      <div class="header-actions">
        <el-icon class="back-btn" :size="24" @click="goBack">
          <ArrowLeft />
        </el-icon>
        <h1 class="page-title">用户主页</h1>
      </div>
      
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <div class="loading-text">加载中...</div>
      </div>
      
      <div v-else-if="userInfo" class="user-info">
        <div class="user-avatar">
          <img :src="userInfo.avatar || '/default-avatar.svg'" :alt="userInfo.nickname" />
        </div>
        <div class="user-details">
          <h2 class="user-nickname">{{ userInfo.nickname }}</h2>
          <p class="user-username">@{{ userInfo.username }}</p>
          <p v-if="userInfo.signature" class="user-signature">
            {{ userInfo.signature }}
          </p>
        </div>
      </div>
    </div>
    
    <!-- 统计信息 -->
    <div v-if="userInfo" class="user-stats">
      <div class="stat-item">
        <div class="stat-number">{{ userVideos.length }}</div>
        <div class="stat-label">作品</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ formatCount(totalLikes) }}</div>
        <div class="stat-label">获赞</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ formatCount(totalViews) }}</div>
        <div class="stat-label">播放</div>
      </div>
    </div>
    
    <!-- 视频列表 -->
    <div class="video-list">
      <div class="list-header">
        <h3>{{ userInfo?.nickname || '用户' }}的作品</h3>
      </div>
      
      <div v-if="videoLoading" class="loading-container">
        <div class="loading-spinner"></div>
        <div class="loading-text">加载视频中...</div>
      </div>
      
      <div v-else-if="userVideos.length === 0" class="empty-container">
        <el-icon :size="60">
          <VideoPlay />
        </el-icon>
        <div class="empty-text">还没有发布作品</div>
      </div>
      
      <div v-else class="video-grid">
        <div
          v-for="video in userVideos"
          :key="video.id"
          class="video-item"
          @click="playVideo(video)"
        >
          <div class="video-cover">
            <img :src="video.coverUrl" :alt="video.title" />
            <div class="video-overlay">
              <el-icon class="play-icon" :size="30">
                <VideoPlay />
              </el-icon>
            </div>
            <div class="video-duration">
              {{ formatDuration(video.duration) }}
            </div>
          </div>
          <div class="video-info">
            <h4 class="video-title">{{ video.title }}</h4>
            <div class="video-stats">
              <span>{{ formatCount(video.viewCount) }} 播放</span>
              <span>{{ formatCount(video.likeCount) }} 点赞</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getUserVideos } from '@/api/video'
import { formatCount, formatDuration } from '@/utils'
import { ElMessage } from 'element-plus'
import { ArrowLeft, VideoPlay } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

const loading = ref(true)
const videoLoading = ref(false)
const userInfo = ref(null)
const userVideos = ref([])

// 计算总点赞数
const totalLikes = computed(() => {
  return userVideos.value.reduce((total, video) => total + video.likeCount, 0)
})

// 计算总播放数
const totalViews = computed(() => {
  return userVideos.value.reduce((total, video) => total + video.viewCount, 0)
})

// 加载用户信息和视频
const loadUserData = async () => {
  const userId = route.params.id
  
  if (!userId) {
    ElMessage.error('用户ID无效')
    goBack()
    return
  }
  
  try {
    loading.value = true
    videoLoading.value = true
    
    // 模拟用户信息（实际项目中应该有获取用户信息的API）
    userInfo.value = {
      id: userId,
      username: `user_${userId}`,
      nickname: `用户${userId}`,
      avatar: '/default-avatar.svg',
      signature: '这个人很懒，什么都没有留下...'
    }
    
    loading.value = false
    
    // 加载用户视频
    const response = await getUserVideos(userId, { current: 1, size: 50 })
    userVideos.value = response.data.records
    
  } catch (error) {
    ElMessage.error('加载用户信息失败')
    console.error('加载用户数据失败:', error)
  } finally {
    loading.value = false
    videoLoading.value = false
  }
}

// 播放视频
const playVideo = (video) => {
  router.push(`/video/${video.id}`)
}

// 返回上一页
const goBack = () => {
  if (window.history.length > 1) {
    router.go(-1)
  } else {
    router.push('/')
  }
}

// 组件挂载
onMounted(() => {
  loadUserData()
})
</script>

<style lang="scss" scoped>
.user-profile-container {
  width: 100vw;
  height: 100vh;
  background: $bg-dark;
  overflow-y: auto;
  padding-bottom: 80px;
}

.user-header {
  position: relative;
  
  .header-actions {
    display: flex;
    align-items: center;
    padding: 20px;
    background: $bg-dark;
    color: $text-white;
    
    .back-btn {
      cursor: pointer;
      transition: $transition-fast;
      
      &:hover {
        transform: translateX(-2px);
      }
    }
    
    .page-title {
      flex: 1;
      text-align: center;
      font-size: 18px;
      font-weight: 600;
      margin: 0;
    }
  }
  
  .user-info {
    padding: 20px;
    background: linear-gradient(135deg, $primary-color 0%, $primary-hover 100%);
    color: $text-white;
    display: flex;
    align-items: center;
    gap: 16px;
    
    .user-avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      border: 3px solid rgba(255, 255, 255, 0.3);
      overflow: hidden;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    
    .user-details {
      flex: 1;
      
      .user-nickname {
        font-size: 24px;
        font-weight: 700;
        margin: 0 0 4px 0;
      }
      
      .user-username {
        font-size: 14px;
        opacity: 0.8;
        margin: 0 0 8px 0;
      }
      
      .user-signature {
        font-size: 14px;
        opacity: 0.9;
        margin: 0;
        line-height: 1.4;
      }
    }
  }
}

.user-stats {
  display: flex;
  background: $text-white;
  
  .stat-item {
    flex: 1;
    text-align: center;
    padding: 20px;
    border-right: 1px solid $border-light;
    
    &:last-child {
      border-right: none;
    }
    
    .stat-number {
      font-size: 20px;
      font-weight: 700;
      color: $text-dark;
      margin-bottom: 4px;
    }
    
    .stat-label {
      font-size: 14px;
      color: $text-gray;
    }
  }
}

.video-list {
  background: $text-white;
  min-height: calc(100vh - 300px);
  
  .list-header {
    padding: 20px;
    border-bottom: 1px solid $border-light;
    
    h3 {
      margin: 0;
      color: $text-dark;
      font-size: 18px;
      font-weight: 600;
    }
  }
  
  .loading-container,
  .empty-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    gap: 16px;
    
    .loading-spinner {
      width: 40px;
      height: 40px;
      border: 3px solid $border-light;
      border-top: 3px solid $primary-color;
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }
    
    .loading-text,
    .empty-text {
      color: $text-gray;
      font-size: 16px;
    }
    
    .el-icon {
      color: $text-gray;
    }
  }
  
  .video-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
    padding: 20px;
    
    .video-item {
      cursor: pointer;
      border-radius: $border-radius;
      overflow: hidden;
      transition: $transition-fast;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: $box-shadow-hover;
        
        .video-overlay {
          opacity: 1;
        }
      }
      
      .video-cover {
        position: relative;
        width: 100%;
        height: 220px;
        background: $bg-gray;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        
        .video-overlay {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          align-items: center;
          justify-content: center;
          opacity: 0;
          transition: $transition-fast;
          
          .play-icon {
            color: $text-white;
          }
        }
        
        .video-duration {
          position: absolute;
          bottom: 8px;
          right: 8px;
          background: rgba(0, 0, 0, 0.7);
          color: $text-white;
          padding: 2px 6px;
          border-radius: 4px;
          font-size: 12px;
        }
      }
      
      .video-info {
        padding: 12px;
        
        .video-title {
          font-size: 14px;
          font-weight: 600;
          color: $text-dark;
          margin: 0 0 8px 0;
          line-height: 1.3;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;
        }
        
        .video-stats {
          display: flex;
          gap: 12px;
          font-size: 12px;
          color: $text-gray;
        }
      }
    }
  }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

// 响应式设计
@media (max-width: 768px) {
  .user-header {
    .user-info {
      padding: 16px;
      
      .user-avatar {
        width: 60px;
        height: 60px;
      }
      
      .user-details {
        .user-nickname {
          font-size: 20px;
        }
      }
    }
  }
  
  .user-stats {
    .stat-item {
      padding: 16px;
      
      .stat-number {
        font-size: 18px;
      }
    }
  }
  
  .video-list {
    .video-grid {
      grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
      gap: 12px;
      padding: 16px;
      
      .video-item .video-cover {
        height: 180px;
      }
    }
  }
}
</style> 